/*
 * MainServerListener.java
 *
 * Created on August 23, 2006, 7:47 PM
 *
 */
package org.letva.copie.subserver.listeners;

import java.io.*;
import java.net.*;
import java.awt.datatransfer.*;
import java.awt.*;
import org.letva.copie.common.message.SubServerInfo;
import org.letva.copie.subserver.*;
import org.letva.copie.common.message.Message;

/**
 *
 * @author Vuk Nikolic
 */
public class MainServerListener extends Thread {

	/** Creates a new instance of MainServerListener */
	public MainServerListener(SubServer ss) {
		super("MainServerListener");
		mySubServer = ss;
		mySubServer.writeLog("MainServerListener created.");
	}

	@Override
	public void run() {
		// prvo da napravimo serverski socket
		try {
			serverSocket = new ServerSocket(mySubServer.getMainServerOnDemandPort());
			mySubServer.writeLog("MainServerListener: Server socket created.");
			// napravili smo serverski socket
		} catch (IOException e) {
			// posto nije uspeo da otvori port za cliente, da bi aktivirali dugme
			// za aktiviranje moram da gasimo i subServerListener
			mySubServer.writeLog("MainServerListener: ERROR: cannot create ServerSocket.");
			mySubServer.stopSubServerListener();
			mySubServer.getMyWindow().setActivateSubServerItem(true);
			theEnd = true;
			mySubServer.getMyWindow().setDialogException("Connection Error", "Cannot connect to port: " + mySubServer.getClientPort());
		}

		// e sada da prihvatimo nove konekcije
		while (!theEnd) {
			try {
				socket = serverSocket.accept(); // blokira se dok neko ne uspostavi vezu
				mySubServer.writeLog("MainServerListener: MainServer connected " + socket.getInetAddress());

				// kako je stigao zahtev od mainServera potrebno je da mu prosledim
				// moj subServerInfo koji ima broj prebacivanja
				Object me = new SubServerInfo(socket.getInetAddress() + "", mySubServer.getMainServerPort());
				((SubServerInfo) me).setSubServerPort(mySubServer.getSubServerPort());
				((SubServerInfo) me).setClientPort(mySubServer.getClientPort());

				// postavi stanje clipboarda
				Clipboard myClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

				if (myClipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
					((SubServerInfo) me).setClipboardStatus(true);
				}

				ObjectOutput out = new ObjectOutputStream(socket.getOutputStream());
				Object outputMessage = new Message("subServer", "mainServer", me);

				out.writeObject(outputMessage);
				out.flush();

				mySubServer.writeLog("MainServerListner: Reply for SubServerThreadOnDemand sent.");
				if (socket != null) {
					socket.close();
				}
			} catch (SocketException ex) {
				mySubServer.writeLog("MainServerListener: SocketExcpetion: " + ex);
				mySubServer.getMyWindow().setDialogException("Connection Error", "Connection problems with client");
			} catch (IOException ex) {
				mySubServer.writeLog("MainServerListener: IOException: " + ex);
				mySubServer.getMyWindow().setDialogException("Connection Error", "Problems with input/output operations");
			}
		}
		mySubServer.writeLog("MainServerListener: end run");
		try {
			if (serverSocket != null) {
				serverSocket.close();
			}

		} catch (IOException ex) {
			ex.printStackTrace();
		}

	}
	boolean theEnd = false;
	// soket ka novom clientu
	private Socket socket = null;
	// serverski soket za klijenta
	private ServerSocket serverSocket = null;
	private SubServer mySubServer = null;
}
